package com.qiwx.pigeon.activity.stock;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.avast.android.dialogs.iface.ISimpleDialogListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.adapter.stock.CustomerAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.network.BaseResponse;
import com.qiwx.pigeon.data.params.FarmPageParam;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.data.vo.stock.CustomerBean;
import com.qiwx.pigeon.helper.DialogHelper;
import com.qiwx.pigeon.presenter.CustomerPresenter;
import com.qiwx.pigeon.viewinterface.ICommonView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020/9/2 3:50 PM
 * 客户管理
 **/
public class CustomerManagerActivity extends BaseMVPActivity<ICommonView<CustomerBean>, CustomerPresenter>
        implements ICommonView<CustomerBean>, ISimpleDialogListener {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.llTitle)
    LinearLayout llTitle;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;

    int pageNum = 1;
    int pageSize = 20;
    boolean isFresh = true;
    int currentPos = 0;
    List<CustomerBean> data = new ArrayList<>();
    FarmPageParam pageParam;
    CustomerAdapter adapter;
    boolean isResumeReFresh = true;//是否要触发刷新

    @Override
    public CustomerPresenter createPresenter() {
        return new CustomerPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_stock_manager_list_layout;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isResumeReFresh) {
            loadData(true);
            isResumeReFresh = false;
        }

    }

    private void initView() {
        titleReturn.changeArrowLeft();
        titleContent.setText("客户管理");
        titleRight.setVisibility(View.VISIBLE);
        titleRight.setText("添加");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.addItemDecoration(new LineItemDecoration(2));
        xRefreshView.setHideFooterWhenComplete(true);
        xRefreshView.setPullLoadEnable(true);
        pageParam = new FarmPageParam();
        pageParam.setF_id(AppContext.getFarmId(this));
        pageParam.setPageSize(pageSize);
        adapter = new CustomerAdapter(R.layout.customer_list_item, data);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                currentPos = position;
                DialogHelper.showCommonDialog((FragmentActivity) context, "确定要删除此记录么?", 1000);

                return false;
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                currentPos = position;
                DialogHelper.showCommonDialog((FragmentActivity) context, "确定拨打电话么?", 1001);
            }

        });
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                loadData(true);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                pageNum++;
                loadData(false);
            }
        });
    }

    //查询出栏记录
    public void loadData(boolean isFresh) {
        this.isFresh = isFresh;
        if (isFresh) {
            data.clear();
            pageNum = 1;
        }
        pageParam.setPageNum(pageNum);
        mPresenter.getCustomerList(pageParam);
    }


    @OnClick({R.id.title_return, R.id.title_right, R.id.txt_nodata, R.id.lin_nodata})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.title_right:
                startActivity(new Intent(context, AddCustomerActivity.class));
                isResumeReFresh = true;
                break;
            case R.id.txt_nodata:
                break;
            case R.id.lin_nodata:
                break;
        }
    }

    @Override
    public void onAddSuccess(BaseResponse response) {

    }

    @Override
    public void onDeleteSuccess(BaseResponse response) {
        MyToast.showCenterShort("删除成功");
        data.remove(currentPos);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onQueryListSuccess(PageInfo<CustomerBean> bean) {
        data.addAll(bean.getList());
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
            if (!bean.isHasNextPage()) {
                MyToast.showLong("没有更多数据了");
            }
        }
        if (!bean.isHasNextPage()) {
            xRefreshView.setPullLoadEnable(false);
        } else {
            xRefreshView.setPullLoadEnable(true);
        }

        if (data == null || data.size() == 0) {
            txtNodata.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
        } else {
            txtNodata.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        switch (requestCode) {
            case 1000:
                mPresenter.deleteCustomer(data.get(currentPos));
                break;
            case 1001:
                if (currentPos < data.size()) {
                    String tel = data.get(currentPos).getTelephone();
                    if (!StringUtil.isEmpty(tel)) {
                        callPhone(tel);
                    } else {
                        MyToast.showCenterShort("联系方式为空");
                    }
                }
                break;
        }

    }
}
